package com.itbour.web.action.com;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.itbour.common.base.BaseAction;
import com.itbour.def.Constants;
import com.itbour.def.MessageCodeConstants;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.com.UserLanguageInfoDto;
import com.itbour.helper.CommonHelper;
import com.itbour.service.com.UserService;
import com.opensymphony.xwork2.ActionContext;

/**
 * copyright pirate alliance team 2014
 *
 * @author zhangjie
 */

/**
 * @author panxuejuan
 *
 */
@SuppressWarnings("serial")
public class UserRegisterWebAction extends BaseAction {
	private UserService userService;

	private UserInfoDto userInfoDto;
	
	private List<UserInfoDto> userList;

	private String email;

	private String password;
	
	private String passwordSure;

	private String username;
	
	private String realname;
	
	private String passwordSureErrorMsg;
	
	public String getPasswordSureErrorMsg() {
		return passwordSureErrorMsg;
	}

	public void setPasswordSureErrorMsg(String passwordSureErrorMsg) {
		this.passwordSureErrorMsg = passwordSureErrorMsg;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	private String userForModify;
	
	private boolean checkFlag;

	public String doInit(){
		this.passwordSureErrorMsg = getText("validation0016");
//		String userId="2014082500000005";
//		userList = userService.getUserListInfo(userId);
//		if(userList != null && userList.size() > 0){
//			setUserListInfo(userList);//用户信息一览
//		}
		return SUCCESS;
	}
	
	//用户信息修改
	public String updateUserInit(){
		String userid=ServletActionContext.getRequest().getParameter("userid");
      this.userInfoDto=this.userService.getServiceUserInfo(userid);
      setUserListInfo(userInfoDto);
      this.userForModify="1";
		return  SUCCESS;
	}

	//用户注册
	public String registerUser()
	{
		boolean isSuccess=true;
		if(password != null && passwordSure !=null && !password.equals(passwordSure)){
			this.addFieldError("passwordSure",getText("validation0016"));
			isSuccess=false;
		}
//		if(selfIntroduction.length()>180){
//			this.addFieldError("selfIntroduction", getText("validation0010"));
//			isSuccess=false;
//		}
		if((email.length()>80)||(password.length()>80)||(username.length()>50)||(realname.length()>50))
		{
			isSuccess=false;
			this.addFieldError("textOverLimit", getText("validation0011"));
		}
		
		if(isSuccess && !checkUsernameMult()){
			isSuccess=false;
			this.addFieldError("usernameMult", getText("validation0015"));
		}
		
		if(isSuccess){
			UserInfoDto userInfo = new UserInfoDto();
			userInfo.setUser_name(username);
			userInfo.setPassword(password);
			userInfo.setEmail(email);
			userInfo.setNickname(realname);
			this.userService.userRegister(userInfo);
			return SUCCESS;
		}
		return INPUT;
	}

	//用户信息修改
	public String updateUser()
	{    
		boolean isSuccess=true;
		if(password != null && passwordSure !=null && !password.equals(passwordSure)){
			this.addFieldError("passwordSure",MessageCodeConstants.E_00000003);
			isSuccess=false;
		}
		
		if((email.length()>80)||(password.length()>80)||(username.length()>50)||(realname.length()>50)){
			isSuccess=false;
			this.addFieldError("textOverLimit", getText("validation0012"));
		}
		
		if(isSuccess && !checkUsernameMult()){
			isSuccess=false;
			this.addFieldError("usernameMult", getText("validation0015"));
		}

		return INPUT;
	}
	
	private void setUserListInfo(UserInfoDto userInfo) {

//			userId=userInfo.getUser_id();
//			nameKanJiSei =userInfo.getName_kanji_sei();
//			nameKanJiMei=userInfo.getName_kanji_mei();
//			nameKatakanaSei=userInfo.getName_katakana_sei();
//			nameKatakanaMei=userInfo.getName_katakana_mei();
//			nameEnFamily=userInfo.getName_en_family();
//			nameEnMiddle=userInfo.getName_en_middle();
//			nameEnLast=userInfo.getName_en_last();
//			String [] birthdayArray=userInfo.getBirthday().split("-");
//			selYear = birthdayArray[0];
//			selMonth = birthdayArray[1];
//			selDay = birthdayArray[2];
//			birthday=userInfo.getBirthday();
//			email=userInfo.getEmail();
//			tel=userInfo.getTel();
//			password=userInfo.getPassword();
//			passwordSure=userInfo.getPassword();
//			sex=userInfo.getSex();
////			selfIntroduction=userInfo.getSelf_introduction();
//			zipCode=userInfo.getZip_code();
//			addProvince=userInfo.getAdd_province();
//			addCity=userInfo.getAdd_city();
//			addOther=userInfo.getAdd_other();
//			loginTime=userInfo.getLogin_time();
//			lastloginTime=userInfo.getLast_login_time();
		}
	
	
	public boolean checkUsernameMult(){
		String username = ServletActionContext.getRequest().getParameter("username");
		//return userService.checkUsernameMult(username);
		return true;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the userInfoDto
	 */
	public UserInfoDto getUserInfoDto() {
		return userInfoDto;
	}

	/**
	 * @param userInfoDto the userInfoDto to set
	 */
	public void setUserInfoDto(UserInfoDto userInfoDto) {
		this.userInfoDto = userInfoDto;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}



	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserForModify() {
		return userForModify;
	}

	public void setUserForModify(String userForModify) {
		this.userForModify = userForModify;
	}

	public List<UserInfoDto> getUserList() {
		return userList;
	}

	public void setUserList(List<UserInfoDto> userList) {
		this.userList = userList;
	}

	public String getPasswordSure() {
		return passwordSure;
	}

	public void setPasswordSure(String passwordSure) {
		this.passwordSure = passwordSure;
	}

	public boolean isCheckFlag() {
		return checkFlag;
	}

	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}

}
